package com.hzdesk.media.koreabatchmatvector.adapter;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hzdesk.media.koreabatchmatvector.R;

import java.security.PublicKey;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018/7/21.
 */

public class ColorPickerAdapter extends RecyclerView.Adapter{
    private List<List<String>> mList;
    private Context mContext;
    private onColorSelectListener onColorSelectListener;

    public void setOnColorSelectListener(ColorPickerAdapter.onColorSelectListener onColorSelectListener) {
        this.onColorSelectListener = onColorSelectListener;
    }

    public ColorPickerAdapter(Context mContext) {
        this.mContext = mContext;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.color_picker_item,parent,false);
        MyViewHoder myViewHoder=new MyViewHoder(view);
        return myViewHoder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof  MyViewHoder){
            MyViewHoder myViewHoder= (MyViewHoder) holder;
            final List<String> list=mList.get(position);
            for (int i=0;i<list.size();i++){
                myViewHoder.list_text.get(i).setBackgroundColor(Color.parseColor("#" + list.get(i)));
                final int finalI = i;
                myViewHoder.list_re.get(i).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (onColorSelectListener!=null){
                            onColorSelectListener.select(list.get(finalI));
                        }
                    }
                });
                myViewHoder.list_re.get(i).setOnHoverListener(onHoverListener);
                myViewHoder.list_re.get(i).setOnFocusChangeListener(onFocusChangeListener);
            }

        }



    }

    public void setDate(List<List<String>> lists){
        mList=lists;
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    private class MyViewHoder extends RecyclerView.ViewHolder{
        private TextView textView_1;
        private TextView textView_2;
        private TextView textView_3;
        private TextView textView_4;
        private TextView textView_5;
        private TextView textView_6;
        private TextView textView_7;
        private TextView textView_8;
        private TextView textView_9;
        private TextView textView_10;
        private TextView textView_11;
        private TextView textView_12;
        private TextView textView_13;
        private TextView textView_14;
        private TextView textView_15;
        private TextView textView_16;
        private TextView textView_17;
        private TextView textView_18;
        private TextView textView_19;
        private TextView textView_20;
        private TextView textView_21;
        private TextView textView_22;
        private TextView textView_23;
        private TextView textView_24;
        private TextView textView_25;
        private TextView textView_26;
        private TextView textView_27;
        private TextView textView_28;
        private TextView textView_29;
        private TextView textView_30;
        private RelativeLayout relativeLayout_1;
        private RelativeLayout relativeLayout_2;
        private RelativeLayout relativeLayout_3;
        private RelativeLayout relativeLayout_4;
        private RelativeLayout relativeLayout_5;
        private RelativeLayout relativeLayout_6;
        private RelativeLayout relativeLayout_7;
        private RelativeLayout relativeLayout_8;
        private RelativeLayout relativeLayout_9;
        private RelativeLayout relativeLayout_10;
        private RelativeLayout relativeLayout_11;
        private RelativeLayout relativeLayout_12;
        private RelativeLayout relativeLayout_13;
        private RelativeLayout relativeLayout_14;
        private RelativeLayout relativeLayout_15;
        private RelativeLayout relativeLayout_16;
        private RelativeLayout relativeLayout_17;
        private RelativeLayout relativeLayout_18;
        private RelativeLayout relativeLayout_19;
        private RelativeLayout relativeLayout_20;
        private RelativeLayout relativeLayout_21;
        private RelativeLayout relativeLayout_22;
        private RelativeLayout relativeLayout_23;
        private RelativeLayout relativeLayout_24;
        private RelativeLayout relativeLayout_25;
        private RelativeLayout relativeLayout_26;
        private RelativeLayout relativeLayout_27;
        private RelativeLayout relativeLayout_28;
        private RelativeLayout relativeLayout_29;
        private RelativeLayout relativeLayout_30;
        public List<TextView> list_text;
        public List<RelativeLayout> list_re;

        @SuppressLint("WrongViewCast")
        public MyViewHoder(View itemView) {
            super(itemView);
            list_text=new ArrayList<>();
            list_re=new ArrayList<>();

            textView_1=itemView.findViewById(R.id.color_picker_tv_1);
            textView_2=itemView.findViewById(R.id.color_picker_tv_2);
            textView_3=itemView.findViewById(R.id.color_picker_tv_3);
            textView_4=itemView.findViewById(R.id.color_picker_tv_4);
            textView_5=itemView.findViewById(R.id.color_picker_tv_5);
            textView_6=itemView.findViewById(R.id.color_picker_tv_6);
            textView_7=itemView.findViewById(R.id.color_picker_tv_7);
            textView_8=itemView.findViewById(R.id.color_picker_tv_8);
            textView_9=itemView.findViewById(R.id.color_picker_tv_9);
            textView_10=itemView.findViewById(R.id.color_picker_tv_10);
            textView_11=itemView.findViewById(R.id.color_picker_tv_11);
            textView_12=itemView.findViewById(R.id.color_picker_tv_12);
            textView_13=itemView.findViewById(R.id.color_picker_tv_13);
            textView_14=itemView.findViewById(R.id.color_picker_tv_14);
            textView_15=itemView.findViewById(R.id.color_picker_tv_15);
            textView_16=itemView.findViewById(R.id.color_picker_tv_16);
            textView_17=itemView.findViewById(R.id.color_picker_tv_17);
            textView_18=itemView.findViewById(R.id.color_picker_tv_18);
            textView_19=itemView.findViewById(R.id.color_picker_tv_19);
            textView_20=itemView.findViewById(R.id.color_picker_tv_20);
            textView_21=itemView.findViewById(R.id.color_picker_tv_21);
            textView_22=itemView.findViewById(R.id.color_picker_tv_22);
            textView_23=itemView.findViewById(R.id.color_picker_tv_23);
            textView_24=itemView.findViewById(R.id.color_picker_tv_24);
            textView_25=itemView.findViewById(R.id.color_picker_tv_25);
            textView_26=itemView.findViewById(R.id.color_picker_tv_26);
            textView_27=itemView.findViewById(R.id.color_picker_tv_27);
            textView_28=itemView.findViewById(R.id.color_picker_tv_28);
            textView_29=itemView.findViewById(R.id.color_picker_tv_29);
            textView_30=itemView.findViewById(R.id.color_picker_tv_30);

            relativeLayout_1=itemView.findViewById(R.id.color_picker_re_1);
            relativeLayout_2=itemView.findViewById(R.id.color_picker_re_2);
            relativeLayout_3=itemView.findViewById(R.id.color_picker_re_3);
            relativeLayout_4=itemView.findViewById(R.id.color_picker_re_4);
            relativeLayout_5=itemView.findViewById(R.id.color_picker_re_5);
            relativeLayout_6=itemView.findViewById(R.id.color_picker_re_6);
            relativeLayout_7=itemView.findViewById(R.id.color_picker_re_7);
            relativeLayout_8=itemView.findViewById(R.id.color_picker_re_8);
            relativeLayout_9=itemView.findViewById(R.id.color_picker_re_9);
            relativeLayout_10=itemView.findViewById(R.id.color_picker_re_10);
            relativeLayout_11=itemView.findViewById(R.id.color_picker_re_11);
            relativeLayout_12=itemView.findViewById(R.id.color_picker_re_12);
            relativeLayout_13=itemView.findViewById(R.id.color_picker_re_13);
            relativeLayout_14=itemView.findViewById(R.id.color_picker_re_14);
            relativeLayout_15=itemView.findViewById(R.id.color_picker_re_15);
            relativeLayout_16=itemView.findViewById(R.id.color_picker_re_16);
            relativeLayout_17=itemView.findViewById(R.id.color_picker_re_17);
            relativeLayout_18=itemView.findViewById(R.id.color_picker_re_18);
            relativeLayout_19=itemView.findViewById(R.id.color_picker_re_19);
            relativeLayout_20=itemView.findViewById(R.id.color_picker_re_20);
            relativeLayout_21=itemView.findViewById(R.id.color_picker_re_21);
            relativeLayout_22=itemView.findViewById(R.id.color_picker_re_22);
            relativeLayout_23=itemView.findViewById(R.id.color_picker_re_23);
            relativeLayout_24=itemView.findViewById(R.id.color_picker_re_24);
            relativeLayout_25=itemView.findViewById(R.id.color_picker_re_25);
            relativeLayout_26=itemView.findViewById(R.id.color_picker_re_26);
            relativeLayout_27=itemView.findViewById(R.id.color_picker_re_27);
            relativeLayout_28=itemView.findViewById(R.id.color_picker_re_28);
            relativeLayout_29=itemView.findViewById(R.id.color_picker_re_29);
            relativeLayout_30=itemView.findViewById(R.id.color_picker_re_30);

            list_text.add(textView_1);
            list_text.add(textView_2);
            list_text.add(textView_3);
            list_text.add(textView_4);
            list_text.add(textView_5);
            list_text.add(textView_6);
            list_text.add(textView_7);
            list_text.add(textView_8);
            list_text.add(textView_9);
            list_text.add(textView_10);
            list_text.add(textView_11);
            list_text.add(textView_12);
            list_text.add(textView_13);
            list_text.add(textView_14);
            list_text.add(textView_15);
            list_text.add(textView_16);
            list_text.add(textView_17);
            list_text.add(textView_18);
            list_text.add(textView_19);
            list_text.add(textView_20);
            list_text.add(textView_21);
            list_text.add(textView_22);
            list_text.add(textView_23);
            list_text.add(textView_24);
            list_text.add(textView_25);
            list_text.add(textView_26);
            list_text.add(textView_27);
            list_text.add(textView_28);
            list_text.add(textView_29);
            list_text.add(textView_30);

            list_re.add(relativeLayout_1);
            list_re.add(relativeLayout_2);
            list_re.add(relativeLayout_3);
            list_re.add(relativeLayout_4);
            list_re.add(relativeLayout_5);
            list_re.add(relativeLayout_6);
            list_re.add(relativeLayout_7);
            list_re.add(relativeLayout_8);
            list_re.add(relativeLayout_9);
            list_re.add(relativeLayout_10);
            list_re.add(relativeLayout_11);
            list_re.add(relativeLayout_12);
            list_re.add(relativeLayout_13);
            list_re.add(relativeLayout_14);
            list_re.add(relativeLayout_15);
            list_re.add(relativeLayout_16);
            list_re.add(relativeLayout_17);
            list_re.add(relativeLayout_18);
            list_re.add(relativeLayout_19);
            list_re.add(relativeLayout_20);
            list_re.add(relativeLayout_21);
            list_re.add(relativeLayout_22);
            list_re.add(relativeLayout_23);
            list_re.add(relativeLayout_24);
            list_re.add(relativeLayout_25);
            list_re.add(relativeLayout_26);
            list_re.add(relativeLayout_27);
            list_re.add(relativeLayout_28);
            list_re.add(relativeLayout_29);
            list_re.add(relativeLayout_30);
        }
    }

    public interface onColorSelectListener{
        void select(String color);
    }

    private View.OnHoverListener onHoverListener = new View.OnHoverListener() {
        @Override
        public boolean onHover(View v, MotionEvent event) {
            switch (event.getAction()) {
                case MotionEvent.ACTION_HOVER_ENTER:
                    v.requestFocus();
                    v.requestFocusFromTouch();
                    break;
                case MotionEvent.ACTION_HOVER_EXIT:
                    break;
                case MotionEvent.ACTION_HOVER_MOVE:
                    break;
            }
            return false;
        }
    };

    private View.OnFocusChangeListener onFocusChangeListener=new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            RelativeLayout linearLayout= (RelativeLayout) v;
            if (hasFocus){
                linearLayout.setBackgroundResource(R.drawable.color_picker_bg_light);

            }else {
                linearLayout.setBackgroundResource(R.drawable.color_picker_bg_drak);

            }

        }
    };


}
